package com.huige.controller;

import com.huige.pojo.User;
import com.huige.service.UserService;
import com.huige.utils.JwtHelper;
import com.huige.utils.Result;
import com.huige.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 朱志辉
 * @Create 2024/7/23 下午2:29
 * @Version 1.0
 * Description:
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtHelper jwtHelper;

    @PostMapping("login")
    public Result login(@RequestBody User user ) {
         Result result =userService.login(user);
        return result;
    }

    @GetMapping("getUserInfo")
    public Result getUserInfo(@RequestHeader("token") String token) {
        Result result = userService.getUserInfo(token);
        return result;
    }


    @PostMapping("checkUserName")
    public Result checkUser(@RequestParam String username) {

        Result result = userService.checkUserName(username);
        return result;
    }


    @PostMapping("regist")
    public Result regist(@RequestBody User user) {
        Result result =userService.regist(user);
        return result;
    }


    @GetMapping("checkLogin")
    public Result checkLogin(@RequestHeader("token") String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration) {
            //已经过期了
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);
    }


}





















